using System;
using System.Reflection;
using System.Text;
using PropertyExpression.Common;

namespace PropertyExpression.PresentationCode.CodeGenerator
{
    internal class AttachedPropertyExtensionsGenerator : IExtensionsGenerator
    {
        private readonly Type type;

        public AttachedPropertyExtensionsGenerator(Type type)
        {
            this.type = type;
        }

        public IOptionalResult<IOverloadedAndPrefixMethodsBuilder> BuildAttachedProperty(string propertyName, StringBuilder builder, MethodInfo methodInfo, Type parameterType, Type attachedMethodTargetType, FieldInfo fieldInfo)
        {
            var methodName = string.Format("{0}_{1}", type.Name, propertyName);
            builder.AppendFormat(
                @"        public static T {0}<T>(this T it, {1} value) where T : {2}
        {{
            {3}.{4}(it, value);
            return it;
        }}
",
                methodName,
                parameterType,
                attachedMethodTargetType,
                type,
                methodInfo.Name);

            return new AttachedPropertyOverloadedAndPrefixMethodsBuilder(methodName, attachedMethodTargetType)
                .AsOptionalResult<IOverloadedAndPrefixMethodsBuilder>();
        }

        public IOptionalResult<IOverloadedAndPrefixMethodsBuilder> BuildSimpleProperty(string propertyName, StringBuilder builder, FieldInfo fieldInfo, Type parameterType)
        {
            return OptionalResult.AsOptionalResult<IOverloadedAndPrefixMethodsBuilder>();
        }
    }
}